﻿using System;
using System.Threading.Tasks;
using FortuneUniverse.Fortune.Algorithms;

namespace FortuneUniverse.Fortune.Dyson
{
  public class PlanetAlgorithm3 : PlanetAlgorithm
  {
    public override void GenerateTerrain(double modX, double modY)
    {
      double num = 0.007;
      double num2 = 0.007;
      double num3 = 0.007;
      URandom1 urandom1 = new URandom1(this.planet.seed);
      int seed1 = urandom1.Next();
      int seed2 = urandom1.Next();
      SimplexNoise simplexNoise = new SimplexNoise(seed1);
      SimplexNoise simplexNoise2 = new SimplexNoise(seed2);
      PlanetRawData data = this.planet.data;
      Parallel.For(0, data.dataLength, (Action<int>) (i =>
      {
        double num1 = (double) data.vertices[i].x * (double) this.planet.radius;
        double num4 = (double) data.vertices[i].y * (double) this.planet.radius;
        double num5 = (double) data.vertices[i].z * (double) this.planet.radius;
        double num6 = num1 + Math.Sin(num4 * 0.15) * 3.0;
        double num7 = num4 + Math.Sin(num5 * 0.15) * 3.0;
        double num8 = num5 + Math.Sin(num6 * 0.15) * 3.0;
        double num9 = simplexNoise.Noise3DFBM(num6 * num * 1.0, num7 * num2 * 1.1, num8 * num3 * 1.0, 6, deltaWLen: 1.8);
        double num10 = simplexNoise2.Noise3DFBM(num6 * num * 1.3 + 0.5, num7 * num2 * 2.8 + 0.2, num8 * num3 * 1.3 + 0.7, 3) * 2.0;
        double num11 = simplexNoise2.Noise3DFBM(num6 * num * 6.0, num7 * num2 * 12.0, num8 * num3 * 6.0, 2) * 2.0;
        double num12 = simplexNoise2.Noise3DFBM(num6 * num * 0.8, num7 * num2 * 0.8, num8 * num3 * 0.8, 2) * 2.0;
        double num13 = num9 * 2.0 + 0.92;
        double num14 = (double) Mathf.Abs((float) num12 + 0.5f);
        double num15 = num10 * num14;
        double f = num13 + (double) Mathf.Clamp01((float) (num15 - 0.35) * 1f);
        if (f < 0.0)
          f *= 2.0;
        double num16 = Maths.Levelize2(f);
        if (num16 > 0.0)
          num16 = Maths.Levelize4(Maths.Levelize2(f));
        double num17 = num16 <= 0.0 ? (double) Mathf.Lerp(-4f, 0.0f, (float) num16 + 1f) : (num16 <= 1.0 ? (double) Mathf.Lerp(0.0f, 0.3f, (float) num16) + num11 * 0.1 : (num16 <= 2.0 ? (double) Mathf.Lerp(0.3f, 1.4f, (float) num16 - 1f) + num11 * 0.12 : (double) Mathf.Lerp(1.4f, 2.7f, (float) num16 - 2f) + num11 * 0.12));
        if (f < 0.0)
          f *= 2.0;
        if (f < 1.0)
          f = Maths.Levelize(f);
        double num18 = num17;
        double num19 = (double) Mathf.Abs((float) f);
        double num20 = num19 <= 0.0 ? 0.0 : (num19 <= 2.0 ? num19 : 2.0);
        double num21 = num20 + (num20 <= 1.8 ? num11 * 0.2 : -num11 * 0.8);
        data.heightData[i] = (ushort) (((double) this.planet.radius + num18 + 0.2) * 100.0);
      }));
    }
  }
}
